<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>Account register</title>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" />                        
  </head>
  <body>
  <?php
  if (isset($_POST['register']))
    {
      include("includes/conf.inc.php");
      
      $user = $_POST['user'];
      $pass = $_POST['pass'];
      
      if ($user=="" || $pass == "") die("Empty username or password!\r\n");
      
      $pass = md5($_POST['pass']);
      
      $id = mysql_connect($db_host,$db_usr,$db_pass) or die('Can\'t connect to the DB server: '.mysql_error());
      mysql_select_db($db_name);
      
      $x = mysql_query("SELECT COUNT(*) FROM $usr_tbl WHERE username = '$user'");
      $a = mysql_fetch_array($x);
      if ($a[0] == 1) die("Username is not available !");
      else
      {
        $query = "INSERT INTO $usr_tbl (username, password) VALUES ('$user', '$pass')";
        mysql_query($query, $id) or die(mysql_error());
        echo "New user registered !";
      }
      
    }
    else
    {
    
    ?>
    
    <script type="text/javascript">
    <!--
      function isEmpty(TextField)
      {
        if (!TextField.value || (TextField.value.length==0 )) 
        {
          return true;
        }
        return false;
      }
      
      function checkform()
      {
         var usrfield = document.regform.user;
         var passfield = document.regform.pass;
         //alert(usrfield.value);
         if (isEmpty(usrfield))
         {
            alert('You haven\'t filled username !');
            usrfield.focus();
            return;
         }
         
         if (isEmpty(passfield))
         {
            alert('You haven\'t filled password !');
            passfield.focus();
            return;
         }
         
         var validcontent = "/^[0-9a-zA-Z[.][_][-]]+$/g";
         
         if (!validcontent.test(userfield.value))
         {
            alert("Illegal char in username");
            usrfield.focus();
            return;
         }
      }
      // -->
    </script>
    <form name="regform" action="register.php" method="post" enctype="multipart/form-data" onsubmit="checkform(); return false;">
    <input type="hidden" name="register" />
    <table>
    <tr>
    <td>username:</td>
    <td><input type="text" id="user" name="user" /></td>
    </tr>
    <tr>
    <td>password:</td>
    <td><input type="password" id="pass" name="pass" /></td>
    </tr>
    <tr>
    <td colspan="2"><input type="submit" value="Create account" /></td>
    </tr>
    </table>
    </form>
    
    <?php
    }
    ?>

</body>
</html>